/**
 * 
 *
 * 
 *
 * 
 */

package com.person.modules.person.controller;


import com.person.common.annotation.SysLog;
import com.person.common.utils.DateUtils;
import com.person.common.utils.R;
import com.person.common.validator.ValidatorUtils;
import com.person.modules.person.entity.CountEntity;
import com.person.modules.person.entity.UserDocEntity;
import com.person.modules.person.service.UserDocEvaluateService;
import com.person.modules.person.service.UserDocService;
import com.person.modules.sys.controller.AbstractController;
import com.person.modules.sys.entity.SysDeptEntity;
import com.person.modules.sys.service.SysDeptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 员工档案
 *
 * @author 
 */
@RestController
	@RequestMapping("/person/doc")
public class UserDocController  extends AbstractController {
	@Autowired
	private UserDocService userDocService;
	@Autowired
	private UserDocEvaluateService userDocEvaluateService;
	@Autowired
	SysDeptService sysDeptService;

	/**
	 * 所有员工档案列表
	 */
	@RequiresPermissions("person:doc:list")
	@CrossOrigin
	@GetMapping("/list")
	@ResponseBody
	public R list(){
		Long companyId = getUser().getCompanyId();
		List<UserDocEntity> list  = userDocService.getDocList(companyId);
		return R.ok().put("list",list);
	}


	/**
	 * 员工档案信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("person:doc:info")
	@ResponseBody
	@CrossOrigin
	public R info(@PathVariable("id") Long id){
		UserDocEntity doc = userDocService.getById(id);
		SysDeptEntity sysDeptEntity =sysDeptService.getById(doc.getDeptId());
		doc.setDeptName(sysDeptEntity.getName());
		return R.ok().put("doc", doc);
	}

	/**
	 * 保存员工档案
	 */
	@SysLog("保存员工档案")
	@RequestMapping("/save")
	@RequiresPermissions("person:doc:save")
	@ResponseBody
	@CrossOrigin
	@Transactional
	public R save( UserDocEntity doc){
		ValidatorUtils.validateEntity(doc);
		doc.setCreateTime(DateUtils.currentTimeFormat() );
		doc.setCompanyId(getCompanyId());
		userDocService.add(doc);
		return R.ok();
	}

	/**
	 * 修改员工档案
	 */
	@SysLog("修改员工档案")
	@RequestMapping("/update")
	@RequiresPermissions("person:doc:update")
	@CrossOrigin
	public R update( UserDocEntity doc){
		ValidatorUtils.validateEntity(doc);
		userDocService.update(doc);
		return R.ok();
	}

	/**
	 * 员工离职 删除员工档案
	 */
	@SysLog("删除员工档案")
	@PostMapping("/delete")
	@RequiresPermissions("person:doc:delete")
	@CrossOrigin
	@ResponseBody
	//传入user_doc的userId
	public R leaveCompany(@Valid @RequestParam(value = "ids",required = false) Long ids){
		userDocService.leaveCompany(ids);
		return R.ok();
	}
	/**
	 * 查询员工
	 */
	@PostMapping("/getList")
/*	@RequiresPermissions("person:doc:delete")*/
	@CrossOrigin
	@ResponseBody
	public R getList(){
		List <UserDocEntity>list=userDocService.queryListByCompanyId(getUser().getCompanyId());

		return R.ok().put("list",list);
	}

	@PostMapping("/investigation")
	@CrossOrigin
	@ResponseBody
	@Transactional
	public  R getUserDocList(String name,String identityCard){
		List <UserDocEntity>list=userDocService.queryUserDocBackground(name,identityCard);
		return R.ok().put("list",list);
	}
	@GetMapping("/getCount")
	@CrossOrigin
	@ResponseBody
	@Transactional
	public  R getCount(){
		CountEntity list =userDocService.getCount(getCompanyId());
		return R.ok().put("list",list);
	}

}